#!/usr/bin/env python
# encoding: utf-8
"""
game.py

Created by Morgan Quirk on 2007-08-09.
"""

import window
import entity
import draw
import common
import random
import pygame
import sys
import physics
import net

class Game:
    def __init__(self):
        self.window=window.Window((800,600))
        self.drawer=draw.Draw(self.window)
        self.ent_manager = entity.EntityManager()
        self.physics = physics.Physics((self.window.x2, self.window.y2), 64)
        self.clock=pygame.time.Clock()
        self.net_client=net.NetClient(self.ent_manager, "127.0.0.1", 9901)
        
    def load(self):
        """Load all images, sounds, etc and initialize objects"""
        common.load_images()
        self.tick_count = 0
        
        self.net_client.send_action("join", "kriuq")

    def run(self):
        """Main game loop"""
        
        while 1:
            self.clock.tick(common.FPS)
            self.tick_count += 1
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()                                                                   
            
            self.net_client.update()

            self.ent_manager.tick()
            if self.tick_count % self.physics.update_rate == 0:
                self.physics.update_grid(self.ent_manager)
            self.physics.tick(self.ent_manager)
            self.drawer.draw_scene(None, self.ent_manager, None)                        
            self.ent_manager.update()
        


def main():
    g=Game()
    g.load()
    g.run()

if __name__ == '__main__':
    main()
